﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class Resizing : Window, IWindow
    {
        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }
  
        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }

        public Resizing()
        {
            InitializeComponent();

            #region Create the rows and columns headers
            HierarchyItem itemAllCountries = dataGrid.RowsHierarchy.Items.Add("All Countries");

            HierarchyItem itemUSA = itemAllCountries.Items.Add("USA");
            ImageSourceConverter converter = new ImageSourceConverter();

            itemUSA.ImageSource = converter.ConvertFromString("pack://application:,,/CSharp/DataGridViewDemos/Images/usa.png") as ImageSource;
            itemUSA.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
            itemUSA.Items.Add("California");
            itemUSA.Items.Add("Oregon");
            itemUSA.Items.Add("Washington");
            itemUSA.Items.Add("Idaho");
            itemUSA.Items.Add("Arizona");
            itemUSA.Items.Add("Montana");
            itemUSA.Items.Add("Alaska");

            HierarchyItem itemCanada = itemAllCountries.Items.Add("Canada");
            itemCanada.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
            itemCanada.ImageSource = converter.ConvertFromString("pack://application:,,/CSharp/DataGridViewDemos/Images/canada.png") as ImageSource;
            itemCanada.Items.Add("Ontario");
            itemCanada.Items.Add("Quebec");
            itemCanada.Items.Add("Nova Scotia");
            itemCanada.Items.Add("Manitoba");
            itemCanada.Items.Add("British Columbia");
            itemCanada.Items.Add("Saskatchewan");
            itemCanada.Items.Add("Alberta");

            HierarchyItem itemMexico = itemAllCountries.Items.Add("Mexico");
            itemMexico.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style; 
            itemMexico.ImageSource = converter.ConvertFromString("pack://application:,,/CSharp/DataGridViewDemos/Images/mexico.png") as ImageSource;
            itemMexico.Items.Add("Baja California");
            itemMexico.Items.Add("Distrito Federal");
            itemMexico.Items.Add("San Luis Potosí");
            itemMexico.Items.Add("Guanajuato");
            itemMexico.Items.Add("Nuevo León");
            itemMexico.Items.Add("Tamaulipas");
            itemMexico.Items.Add("Sonora");

            HierarchyItem itemAllCustomers = dataGrid.ColumnsHierarchy.Items.Add("All Customers");
            itemAllCustomers.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
            itemAllCustomers.Items.Add("Graduate degree");
            itemAllCustomers.Items.Add("Bachelors degree");
            itemAllCustomers.Items.Add("Associate degree");
            itemAllCustomers.Items.Add("High School diploma");
            HierarchyItem itemTotal = itemAllCustomers.Items.Add("Total");

            #endregion
            dataGrid.RowsHierarchy.CompactStyleRenderingEnabled = true;

            dataGrid.RowsHierarchy.ExpandAllItems();
            dataGrid.ColumnsHierarchy.ExpandAllItems();

            dataGrid.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ITEM_CONTENT);
            dataGrid.RowsHierarchy.SetColumnWidth(0, 130);
            itemTotal.Width = 130;

            #region Load the cells data
            Random rand = new Random();
            for (int iCountry = 0; iCountry < itemAllCountries.Items.Count; iCountry++)
            {
                HierarchyItem country = itemAllCountries.Items[iCountry];
                country.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
       
                foreach (HierarchyItem state in country.Items)
                {
                    state.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
       
                    foreach (HierarchyItem degree in dataGrid.ColumnsHierarchy.Items[0].Items)
                    {
                        degree.CellStyle = Application.Current.Resources["TextCenteredCellStyle"] as Style;
                        if (degree.Text == "Total")
                            break;
                        double value = (double)rand.Next(100, 2000);
                        dataGrid.CellsArea.SetCellValue(state, degree, value);
                    }
                }
            }

            foreach (HierarchyItem degree in itemAllCustomers.Items)
            {
                if (degree.Text == "Total")
                    break;

                RollupData(degree, dataGrid.RowsHierarchy.Items[0]);
            }

            // Make the totals column virtual
            itemAllCustomers.CellDataSource = GridCellDataSource.Virtual;
            itemAllCustomers.Items[4].CellDataSource = GridCellDataSource.Virtual;

            dataGrid.CellValueNeeded += new VIBlend.WPF.Controls.DataGrid.CellValueNeededEventHandler(dataGrid_CellValueNeeded);

            #endregion

            dataGrid.RowsHierarchy.AllowResize = true;
            dataGrid.ColumnsHierarchy.AllowResize = true;

            this.dataGrid.Refresh();
        }

        void dataGrid_CellValueNeeded(object sender, CellValueNeededEventArgs args)
        {
            if (args.ColumnItem.Text == "Total")
            {
                double value = 0;
                foreach (HierarchyItem column in args.ColumnItem.ParentItem.Items)
                {
                    if (column == args.ColumnItem)
                        break;

                    object cellValue = dataGrid.CellsArea.GetCellValue(args.RowItem, column);
                    if (cellValue != null && cellValue.GetType() == typeof(double))
                        value += (double)cellValue;
                }

                args.CellValue = value;
            }
            else if (args.ColumnItem.Text == "All Customers")
            {
                // get the value from the total columns which is the 5th one in the child items list
                args.CellValue = dataGrid.CellsArea.GetCellValue(args.RowItem, args.ColumnItem.Items[4]);
            }
        }

        private void RollupData(HierarchyItem columnItem, HierarchyItem rowItem)
        {
            double value = 0;
            foreach (HierarchyItem item in rowItem.Items)
            {
                if (item.Items.Count > 0)
                    RollupData(columnItem, item);

                value += (double)dataGrid.CellsArea.GetCellValue(item, columnItem);
            }

            dataGrid.CellsArea.SetCellValue(rowItem, columnItem, value);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            this.dataGrid.CellValueNeeded -= new VIBlend.WPF.Controls.DataGrid.CellValueNeededEventHandler(dataGrid_CellValueNeeded);
            this.dataGrid.Dispose();
            this.dataGrid = null;
        }
    }
}
